package com.example.duomeiti;


import androidx.appcompat.app.AppCompatActivity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MusicActivity extends AppCompatActivity {
    SeekBar seekBar;//进度条
    Button play,stop,exit;
    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);
        seekBar=findViewById(R.id.seekbat);
        play=findViewById(R.id.play);
        stop=findViewById(R.id.stop);
        exit=findViewById(R.id.exit);
        mediaPlayer=MediaPlayer.create(this,R.raw.music);
        mediaPlayer.setLooping(true);

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //播放按钮
                try {
                    if (!mediaPlayer.isPlaying()) { //如果没有正在播放
                        //播放事件
                        mediaPlayer.start();
                        play.setText("暂停");
                        //音乐在播放时，进度条跟着动
                        Timer timer=new Timer();
                        //周期性做一个事情
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                seekBar.setProgress(mediaPlayer.getCurrentPosition());
                            }
                        },0,50);
                    }else{                          //正在播放
                        //暂停事件
                        mediaPlayer.pause();
                        play.setText("播放");
                    }
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(),"播放错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //停止按钮
                mediaPlayer.stop();
                mediaPlayer.reset();
                play.setText("播放");
                mediaPlayer=MediaPlayer.create(getApplicationContext(),R.raw.abc);
                mediaPlayer.setLooping(true);
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //退出按钮
                MusicActivity.this.finish();
            }
        });
        //准备的一个监听
        mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                seekBar.setMax(mediaPlayer.getDuration());
            }
        });
        //进度条发生改变触发的事件
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            private boolean isSeekBarChanging;
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                //进度条改变完的时候，要做的事情

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //进度条正在拖动时候要做的事情
                isSeekBarChanging=true;
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //进度条停止拖动的时候要做的事情
                isSeekBarChanging=false;
                mediaPlayer.seekTo(seekBar.getProgress());
            }
        });
    }
}